﻿<%@ WebHandler Language="C#" Class="QQPWD" %>

using System;
using System.Web;
using System.Web.Security;
using System.Security.Cryptography;

public class QQPWD : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) 
    {
        MD5 md = new MD5CryptoServiceProvider();
        context.Response.Write(md);
        string pwd = context.Request.QueryString["s"];
        if (pwd == null || pwd.Length == 0)
        {
            return;
        }
        context.Response.ContentType = "text/html";
        char[] md5 = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "md5").ToCharArray();
        context.Response.Write(new string(md5)+"<br>");
        string base64 = "";
        int c1, c2, c3,c4;
        for (int i = 0; i < md5.Length; i+=6)
        {
            c2 =c3=c4= -1;
            c1=getInt(md5[i])<<2;
            if (i < md5.Length - 1)
            {
                c1 += getInt(md5[i + 1]) >> 2;
                c2 = (getInt(md5[i + 1]) & 3) << 4;
                if (i < md5.Length - 2)
                {
                    c2 += getInt(md5[i + 2]);
                    if (i < md5.Length - 3)
                    {
                        c3 = getInt(md5[i + 3]) << 2;
                        if (i < md5.Length - 4)
                        {
                            c3 += getInt(md5[i + 4]) >> 2;
                            c4 = (getInt(md5[i + 4]) & 3) << 4;
                            if (i < md5.Length - 5)
                            {
                                c4 += getInt(md5[i + 5]);
                            }
                        }
                    }
                }
            }
            base64 = base64 + getChar(c1) + getChar(c2) + getChar(c3) + getChar(c4);
        }
        context.Response.Write(base64);
    }

    private int getInt(char c)
    {
        if (c >= '0' && c <= '9')
        {
            return c - 48;
        }
        else
        {
            return c - 55;
        }
    }
    private char getChar(int c)
    {
        if (c == -1)
        {
            return '=';
        }
        if ( c < 26)
        {
            return (char)(c + 65);
        }
        else if(c<52)
        {
            return (char)(c + 71);
        }
        else if (c < 62)
        {
            return (char)(c -4);
        }
        else if (c == 62)
        {
            return '+';
        }
        else if (c == 63)
        {
            return '/';
        }
        else
        {
            return '=';
        }
    }
    public bool IsReusable 
    {
        get {
            return false;
        }
    }

}